package com.zhw.goshopping.content.service;

import com.zhw.goshopping.common.pojo.EasyUITreeNode;
import com.zhw.goshopping.common.pojo.GoShoppingResult;
import com.zhw.goshopping.dao.TbContentCategoryMapper;
import com.zhw.goshopping.pojo.TbContentCategory;
import com.zhw.goshopping.pojo.TbContentCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by zhaihongwei on 2018/3/24
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<EasyUITreeNode> getContentCategoryList(long parentId) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> categoryList = contentCategoryMapper.selectByExample(example);
        List<EasyUITreeNode> resultList = new ArrayList<>();
        for (TbContentCategory tbContentCategory : categoryList) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setText(tbContentCategory.getName());
            node.setId(tbContentCategory.getId());
            node.setState(tbContentCategory.getIsParent()?"closed":"open");
            resultList.add(node);
        }
        return resultList;
    }

    @Override
    public GoShoppingResult addContentCategory(long parentId, String name) {
        // 创建pojo对象
        TbContentCategory tbContentCategory = new TbContentCategory();
        // 补全对象属性
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setName(name);
        // 状态。1、正常，2、删除
        tbContentCategory.setStatus(1);
        // 排序，默认为1
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setIsParent(false);
        Date date = new Date();
        tbContentCategory.setCreated(date);
        tbContentCategory.setUpdated(date);
        // 插入数据库
        contentCategoryMapper.insert(tbContentCategory);
        // 判断父节点的状态
        TbContentCategory parent = contentCategoryMapper.selectByPrimaryKey(parentId);
        if (!parent.getIsParent()) {
            // 如果父节点为叶子节点，改为父节点
            parent.setIsParent(true);
            // 根性父节点
            contentCategoryMapper.updateByPrimaryKey(parent);
        }
        // 返回结果
        return GoShoppingResult.ok();
    }

    @Override
    public void updateContentCategory(long id, String name) {
        TbContentCategory tbContentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        tbContentCategory.setName(name);
        contentCategoryMapper.updateByPrimaryKey(tbContentCategory);
    }

    @Override
    public boolean deleteContentCategory(long id) {
        TbContentCategory tbContentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        if (tbContentCategory.getIsParent()) {
            // 是父节点，不允许删除
            return false;
        }else {
            // 不是父节点，直接删除
            contentCategoryMapper.deleteByPrimaryKey(id);
            return true;
        }
    }
}
